/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Evaluador;
import modelo.ExperienciaAcademica;
import modelo.ExperienciaAcademicaPK;

/**
 *
 * @author Fabian
 */
public class ExperienciaAcademicaJpaController implements Serializable {

    public ExperienciaAcademicaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ExperienciaAcademica experienciaAcademica) throws PreexistingEntityException, Exception {
        if (experienciaAcademica.getExperienciaAcademicaPK() == null) {
            experienciaAcademica.setExperienciaAcademicaPK(new ExperienciaAcademicaPK());
        }
        experienciaAcademica.getExperienciaAcademicaPK().setCedulaEvaluador(experienciaAcademica.getEvaluador().getCedula());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Evaluador evaluador = experienciaAcademica.getEvaluador();
            if (evaluador != null) {
                evaluador = em.getReference(evaluador.getClass(), evaluador.getCedula());
                experienciaAcademica.setEvaluador(evaluador);
            }
            em.persist(experienciaAcademica);
            if (evaluador != null) {
                evaluador.getExperienciaAcademicaList().add(experienciaAcademica);
                evaluador = em.merge(evaluador);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExperienciaAcademica(experienciaAcademica.getExperienciaAcademicaPK()) != null) {
                throw new PreexistingEntityException("ExperienciaAcademica " + experienciaAcademica + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ExperienciaAcademica experienciaAcademica) throws NonexistentEntityException, Exception {
        experienciaAcademica.getExperienciaAcademicaPK().setCedulaEvaluador(experienciaAcademica.getEvaluador().getCedula());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ExperienciaAcademica persistentExperienciaAcademica = em.find(ExperienciaAcademica.class, experienciaAcademica.getExperienciaAcademicaPK());
            Evaluador evaluadorOld = persistentExperienciaAcademica.getEvaluador();
            Evaluador evaluadorNew = experienciaAcademica.getEvaluador();
            if (evaluadorNew != null) {
                evaluadorNew = em.getReference(evaluadorNew.getClass(), evaluadorNew.getCedula());
                experienciaAcademica.setEvaluador(evaluadorNew);
            }
            experienciaAcademica = em.merge(experienciaAcademica);
            if (evaluadorOld != null && !evaluadorOld.equals(evaluadorNew)) {
                evaluadorOld.getExperienciaAcademicaList().remove(experienciaAcademica);
                evaluadorOld = em.merge(evaluadorOld);
            }
            if (evaluadorNew != null && !evaluadorNew.equals(evaluadorOld)) {
                evaluadorNew.getExperienciaAcademicaList().add(experienciaAcademica);
                evaluadorNew = em.merge(evaluadorNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ExperienciaAcademicaPK id = experienciaAcademica.getExperienciaAcademicaPK();
                if (findExperienciaAcademica(id) == null) {
                    throw new NonexistentEntityException("The experienciaAcademica with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ExperienciaAcademicaPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ExperienciaAcademica experienciaAcademica;
            try {
                experienciaAcademica = em.getReference(ExperienciaAcademica.class, id);
                experienciaAcademica.getExperienciaAcademicaPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The experienciaAcademica with id " + id + " no longer exists.", enfe);
            }
            Evaluador evaluador = experienciaAcademica.getEvaluador();
            if (evaluador != null) {
                evaluador.getExperienciaAcademicaList().remove(experienciaAcademica);
                evaluador = em.merge(evaluador);
            }
            em.remove(experienciaAcademica);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ExperienciaAcademica> findExperienciaAcademicaEntities() {
        return findExperienciaAcademicaEntities(true, -1, -1);
    }

    public List<ExperienciaAcademica> findExperienciaAcademicaEntities(int maxResults, int firstResult) {
        return findExperienciaAcademicaEntities(false, maxResults, firstResult);
    }

    private List<ExperienciaAcademica> findExperienciaAcademicaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ExperienciaAcademica.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ExperienciaAcademica findExperienciaAcademica(ExperienciaAcademicaPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ExperienciaAcademica.class, id);
        } finally {
            em.close();
        }
    }

    public int getExperienciaAcademicaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ExperienciaAcademica> rt = cq.from(ExperienciaAcademica.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
